home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / flcpp2.zip / FLEXLIST.HPP < prev    next >
C/C++ Source or Header  |  1990-11-28  |  6KB  |  211 lines

  1. /*
  2.     flexlist.hpp
  3.     11-28-90
  4.     Homogeneous-heterogeneous
  5.     hybrid stack-queue-list-array generic class.
  6.     C++ versions 1 & 2
  7.  
  8.     Copyright 1990
  9.     John W. Small
  10.     All rights reserved
  11.  
  12.     PSW / Power SoftWare
  13.     P.O. Box 10072
  14.     McLean, Virginia 22102 8072
  15.     (703) 759-3838
  16.  
  17. */
  18.  
  19. #ifndef FLEXLIST_CPP
  20. #define FLEXLIST_CPP
  21.  
  22. //  Define CPP1 for C++ version 1 compatibility
  23. // #define CPP1
  24.  
  25. #ifdef    CPP1
  26. #define protected  public
  27. #endif
  28.  
  29. //  Some compilers have ANSI C's standard library available
  30. //  Define ANSI_C_STD_LIB to use this library
  31. // #define ANSI_C_STD_LIB
  32. #ifdef    ANSI_C_STD_LIB
  33. #include <stddef.h>    /*  size_t    */
  34. #include <limits.h>    /*  UINT_MAX  */
  35. #include <string.h>    /*  strcmp(), memcpy(), memset()  */
  36. #else
  37. typedef    unsigned size_t;
  38. #define UINT_MAX  -1
  39. extern int strcmp(const char *s1, const char *s2);
  40. extern void *memcpy(void *dest, const void *src, size_t n);
  41. extern void *memset(void *s, int c, size_t n);
  42. #endif
  43.  
  44. // FLvariantData specifies that the size of the data
  45. // stored within the FlexNode is to be determined by
  46. // the variant FlexNode virtual functions found in the
  47. // FlexList class.  See the first FlexList constructor.
  48. #define FLvariantData  0
  49. // In the FlexList class the virtual functions are coded
  50. // to handle C strings.  To construct a FlexList to
  51. // hold variant length FlexNodes housing C strings
  52. // code: FlexList s(FLstrings);
  53. #define FLstrings  FLvariantData
  54.  
  55. // FLmaxMaxNodes specifies the maximum number of FlexNodes
  56. // allowed in any FlexList.
  57. #define FLmaxMaxNodes  UINT_MAX
  58.  
  59. class    FlexList;
  60. typedef    FlexList *FlexL;
  61. #define FlexL0  ((FlexL)0)
  62. class    FlexNode;
  63. typedef    FlexNode *FlexN;
  64. #define FlexN0  ((FlexN)0)
  65.  
  66. class FlexNode  {
  67.     FlexN  next, prev;
  68.     friend FlexList;
  69. public:
  70.     char data[1];
  71. };
  72.  
  73. // FLcomparE() typecasts a compare function pointer to
  74. // the type required by FlexList::setCompare() and
  75. // FlexList::sort().
  76. #define FLcomparE(compare)  ((int (*)(const void *D1, \
  77.         const void *D2)) compare)
  78. // FLcompare0 is the NULL compare function pointer.
  79. #define FLcompare0  FLcomparE(0)
  80.  
  81.  
  82. class FlexList {
  83.     FlexN  front, current, rear;
  84.     unsigned  curNum, nodes, maxNodes;
  85.     size_t sizeofNodeData, sizeofNode;
  86.     int sorted;
  87.     int (*compare)(const void *D1, const void *D2);
  88. protected:
  89.     // Variant FlexNode virtual functions:
  90.     // these functions are setup  in the FlexList base
  91.     // class to handle C strings.  A FlexNode in the
  92.     // FlexList will only be as big as necessary to 
  93.     // accomodate the string stored within (see first
  94.     // FlexList constructor).  Derive a new class from
  95.     // FlexList and redefine these virtual functions to
  96.     // accomodate your specific variant data.
  97.     virtual FlexN FNnew(const void *D);
  98.     virtual int   FNwrite(void *ND, const void *D);
  99.     virtual int   FNread(const void *ND, void *D);
  100.     virtual int   FNdestruct(void *ND, void *D);
  101. public:
  102.     // FlexList constructors:
  103.     // sizeofNodeData specifies the size of data to be
  104.     // stored within the nodes of the FlexList.  If
  105.     // sizeofNodeData is equal to FLvariantData, i.e. 0,
  106.     // then the variant FlexNode virtual functions above
  107.     // are used to accomodate variant length data.
  108.     FlexList(size_t sizeofNodeData, unsigned maxNodes
  109.         = FLmaxMaxNodes);
  110.     //  Unpack array into FlexList
  111.     FlexList(unsigned sizeofCell, unsigned cells,
  112.         const void *array);
  113.  
  114.     // FlexList destructors:
  115.     int   clear();
  116.     virtual ~FlexList() { clear(); }
  117.  
  118.     // FlexList header functions:
  119.     void *frontD() { return front? front->data : 0; }
  120.     void *currentD() 
  121.         { return current? current->data : 0; }
  122.     void *rearD() { return rear? rear->data : 0; }
  123.     unsigned CurNum() { return curNum; }
  124.     unsigned Nodes() { return nodes; }
  125.     unsigned MaxNodes() { return maxNodes; }
  126.     int   setMaxNodes(unsigned maxNodes = FLmaxMaxNodes)
  127.         { return ((maxNodes >= nodes)? 
  128.         this->maxNodes = maxNodes, 1 : 0); }
  129.     unsigned notFull() { return (maxNodes - nodes); }
  130.     unsigned SizeofNodeData() { return sizeofNodeData; }
  131.     int   isSorted() { return sorted; }
  132.     int   unSort()  { sorted = 0; return 1; }
  133.     int (*Compare())(const void *D1, const void *D2) 
  134.         { return compare; }
  135.     void  setCompare(int (*compare)
  136.         (const void *D1, const void *D2))
  137.         { this->compare = compare; sorted = 0; }
  138.     unsigned isFixed() { return sizeofNodeData; }
  139.     unsigned isVariant() { return !sizeofNodeData; }
  140.  
  141.     // FlexList stack and queue functions:
  142.     void *pushN(FlexN N);
  143.     void *pushD(const void *D = 0);
  144.     FlexN popN();
  145.     int   popD(void *D = 0);
  146.     void *topD(void *D = 0);
  147.     void *insQN(FlexN N);
  148.     void *insQD(const void *D = 0);
  149.  
  150.     // FlexList list functions:
  151.     void *mkcur(unsigned n = 0);
  152.     void *insN(FlexN N);
  153.     void *insD(const void *D = 0);
  154.     void *insSortN(FlexN N);
  155.     void *insSortD(const void *D); 
  156.     FlexN delN();
  157.     int   delD(void *D = 0);
  158.     void *nextD(void *D = 0);
  159.     void *prevD(void *D = 0);
  160.  
  161.     // FlexList search/sort functions:
  162.     // (see also insSortN()/insSortD() list functions)
  163.     void *findFirstD(const void *D);
  164.     void *findNextD(const void *D);
  165.     void *findLastD(const void *D);
  166.     void *findPrevD(const void *D);
  167.     int   sort(int (*compare)
  168.         (const void *D1, const void *D2) = 0);
  169.  
  170.     // FlexList array functions:
  171.     // See also compaction functions
  172.     int   storeD(const void *D, unsigned n = 0);
  173.     int   recallD(void *D, unsigned n = 0);
  174.  
  175.     // FlexList compaction functions:
  176.     // (see also FlexList constructors)
  177.     void *pack();
  178.     void **packPtrs();
  179. };
  180.  
  181.  
  182. /*  
  183.     FlexList implementation constants -
  184.     change as required by target machine.
  185. */
  186.  
  187.  
  188. // Segmented machine architectures prevent the maximum
  189. // segment size from being allocated with new.  I've assumed
  190. // a loss of 16 bytes to be the worst case for all machines.
  191. // FLnewAlignLoss is used in macros below.
  192. #define FLnewAlignLoss  16
  193.  
  194. // FLmaxSizeofNodeData is used in FlexList constructors to
  195. // specify the maximum sizeof(data) that can be stored in
  196. // a FlexNode.
  197. #define FLmaxSizeofNodeData  \
  198.     ((size_t)(-(long)sizeof(FlexNode) \
  199.     -FLnewAlignLoss))
  200.  
  201. // FLmaxSizeofArray is used in pack() and packPtrs().
  202. // Many compilers truncate long to unsigned on calls to new!
  203. #define FLmaxSizeofArray ((long)(size_t)-FLnewAlignLoss)
  204. // If your machine and compiler can handle full long
  205. // allocations then you can redefine FLmaxSizeofArray as:
  206. // #define FLmaxSizeofArray sizeofArray
  207. // This allows full range positive long values.
  208.  
  209.  
  210. #endif
  211.